package org.locate.server;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

import oauth.signpost.OAuth;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.OAuthProvider;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.basic.DefaultOAuthProvider;
import oauth.signpost.exception.OAuthCommunicationException;
import oauth.signpost.exception.OAuthExpectationFailedException;
import oauth.signpost.exception.OAuthMessageSignerException;
import oauth.signpost.exception.OAuthNotAuthorizedException;

public class FoursquareMgr {

	private static final String FOURSQUARE_OAUTH_AUTHORIZE = "http://foursquare.com/oauth/authorize";
	private static final String FOURSQUARE_OAUTH_ACCESS_TOKEN = "http://foursquare.com/oauth/access_token";
	private static final String FOURSQUARE_OAUTH_REQUEST_TOKEN = "http://foursquare.com/oauth/request_token";

	private static final String CONSUMER_KEY = "305W2RVK2EDRW150N3XZ0EEPJMSGMKXSJ5MGVACBSR5IOWX0";
	private static final String CONSUMER_SECRET = "1CCOECYY5Y11MTECXKIJWDLD3U5HHDFPNXRJNXUKDVZCSCNC";
		
	private OAuthConsumer consumer = new DefaultOAuthConsumer(CONSUMER_KEY,
			CONSUMER_SECRET);
	private OAuthProvider provider = new DefaultOAuthProvider(
			FOURSQUARE_OAUTH_REQUEST_TOKEN, FOURSQUARE_OAUTH_ACCESS_TOKEN,
			FOURSQUARE_OAUTH_AUTHORIZE);

	public String connect() throws OAuthMessageSignerException,
			OAuthNotAuthorizedException, OAuthExpectationFailedException,
			OAuthCommunicationException {

		String authURL = provider.retrieveRequestToken(consumer,
				OAuth.OUT_OF_BAND);

		return authURL;
	}

	public AccessToken requestAccessToken(String pinCode)
			throws OAuthMessageSignerException, OAuthNotAuthorizedException,
			OAuthExpectationFailedException, OAuthCommunicationException {

		provider.retrieveAccessToken(consumer, pinCode);
		String token = consumer.getToken();
		String tokenSecret = consumer.getTokenSecret();

		AccessToken accessToken = new AccessToken();
		accessToken.setToken(token);
		accessToken.setTokenSecret(tokenSecret);
		accessToken.setPinCode(pinCode);

		return accessToken;

	}

	private String readRequest(InputStream inputStream) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(
				inputStream));

		String inputLine;
		StringBuffer response = new StringBuffer();

		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();

		return response.toString();

	}

	public String getNearbyPlaces(String pinCode, String geolat, String geolong)
			throws IOException, OAuthMessageSignerException,
			OAuthNotAuthorizedException, OAuthExpectationFailedException,
			OAuthCommunicationException {

		// create an HTTP request to a protected resource
		URL url = new URL("http://api.foursquare.com/v1/venues.json?geolat="
				+ geolat + "&geolong=" + geolong);
		HttpURLConnection request = (HttpURLConnection) url.openConnection();

		// sign the request

		consumer.sign(request);

		// send the request
		request.connect();

		return readRequest(request.getInputStream());

	}

	public String checkin(AccessToken accessToken, FoursquareForm form)
			throws IOException, OAuthMessageSignerException,
			OAuthExpectationFailedException, OAuthCommunicationException {

		String venueName = form.getVenueName();
		String shout = URLEncoder.encode(form.getShout(), "UTF-16");

		// create an HTTP request to a protected resource
		URL url = new URL("http://api.foursquare.com/v1/checkin.json?geolat="
				+ form.getGeolat() + "&geolong=" + form.getGeolong()
				+ "&shout=" + shout + "&venue=" + venueName);
		HttpURLConnection request = (HttpURLConnection) url.openConnection();
		request.setRequestMethod("POST");

		// sign the request
		consumer.setTokenWithSecret(accessToken.getToken(), accessToken
				.getTokenSecret());
		consumer.sign(request);

		// send the request
		request.connect();

		return readRequest(request.getInputStream());
	}

	public String tips(AccessToken accessToken, FoursquareForm form)
			throws IOException, OAuthMessageSignerException,
			OAuthExpectationFailedException, OAuthCommunicationException {
		
		String addr = "http://api.foursquare.com/v1/tips.json?geolat="
				+ form.getGeolat() + "&geolong=" + form.getGeolong();

		URL url = new URL(addr);

		HttpURLConnection request = (HttpURLConnection) url.openConnection();
		request.setRequestMethod("GET");

		// sign the request
		consumer.setTokenWithSecret(accessToken.getToken(), accessToken
				.getTokenSecret());
		consumer.sign(request);

		// send the request
		request.connect();

		String output = readRequest(request.getInputStream());
		
		
		return output;
	}

}
